<?php
	include("head.php");
	include_once("paging_function.php");
?><script>
function changeUrl(indexalpha)
{
var url='network.php?index='+indexalpha;
window.location=url;
}

</script>
<?php
/////////////////////////////////PAGING /////////////////
 	if(isset($_GET['page']) && $_GET['page']!='')
		{
		
			$page = $_GET['page'];
		}
	else
		{
			$page = 0;
		}
$limit=$adminpagelimit;
/////////////////////////////////////////////////////////////////	
?>
<?php
////////////////////////////////////////////    Block_---- DELETE  ///////////
if(isset($_POST['Submit']))
{
	$del=$_POST['del'];
	$no=count($del);
	if($no>0)
	{
	for($i=0;$i<$no;$i++)
	{
	   mysql_query("DELETE FROM ".NETWORKMASTER."  WHERE networkId= '".$del[$i]."' ");	   
	}
	echo '<script>window.location="network.php?dlt=1"</script>';
	}
	$block=$_POST['count_UPDATE'];
	if($_POST['count_UPDATE']>0)
	{
	for($no_up=0; $no_up<$block;$no_up++)
	{
	if(isset($_POST['block'.$no_up]))
	{
	 mysql_query("UPDATE ".NETWORKMASTER."  set status='N' WHERE networkId='".$_POST['block'.$no_up]."'");
	}else{
	 mysql_query("UPDATE ".NETWORKMASTER."  set status='Y' WHERE networkId='".$_POST['block_hid'.$no_up]."'");
	}
	}
	echo '<script>window.location="network.php?page='.$_GET['page'].'&loop='.$_GET['loop'].'&start='.$_GET['start'].'&blk=1"</script>';
	}
}

?>
<?php 
//////////////////////////////////////// insert edit query====================//////////
if(isset($_REQUEST['submit1']))
	{
		if($_REQUEST['action']=='create')
			{
			$insert_query=mysql_query("INSERT INTO ".NETWORKMASTER." SET `networkName`='".$_POST['name']."',`networkType`= '".$_POST['type']."',`siteuserId`='0',`networkBidType`='".$_REQUEST['bidtype']."' ,`status`='".$_POST['status']."'"
			) or die('NETWORKMASTER '.mysql_error());


//echo "INSERT INTO ".NETWORKMASTER." SET `networkName`='".$_POST['name']."',`networkType`= '".$_POST['type']."',`siteuserId`='0',`networkBidType`='".$_REQUEST['bidtype']."',`status`='".$_POST['status']."'"."<br><br>";


			$lastqueryid=mysql_insert_id();

			$insertdetailssql="INSERT INTO ".NETWORKDETAILS." SET `networkId`='".$lastqueryid."',`networkMinPrice`='".$_POST['networkprice']."',`costPerBidType`='".$_POST['bidcost']."'";
//echo $insertdetailssql;
			$insertquerydetails=mysql_query($insertdetailssql) or die('NETWORKDETAILS '.mysql_error());

			$inertsqlnetcount="INSERT INTO ".NETCOUNTRY." SET networkId='".$lastqueryid."',`countryAbbr`='".$_POST['country']."' ";

//echo $inertsqlnetcount;
			$inertquerynetcount=mysql_query($inertsqlnetcount)  or die('NETCOUNTRY '.mysql_error());

			$INSERTDETAILSSQL="INSERT INTO `".NETADDETAILS."` SET `networkId`='".$lastqueryid."'";
//echo $INSERTDETAILSSQL;
			$INSERTDETAILS=mysql_query($INSERTDETAILSSQL) or die('NETADDETAILS '.mysql_error());
			echo '<script> window.location="network.php?inst=1"</script>';
			}

		if($_REQUEST['action']=='edit')
			{
// 			$updatesql="UPDATE `".NETWORKMASTER."`,`".NETWORKDETAILS."`,`".NETCOUNTRY."` SET `".NETWORKMASTER."`.`networkName` = '".$_POST['name']."',`".NETWORKMASTER."`.`networkType` = '".$_POST['type']."',`".NETWORKMASTER."`.`networkBidType`='".$_REQUEST['bidtype']."',`".NETWORKMASTER."`.`status`='".$_POST['status']."',`".NETWORKDETAILS."`.`networkMinPrice`='".$_POST['networkprice']."',`".NETWORKDETAILS."`.`costPerBidType`='".$_POST['bidcost']."',`".NETCOUNTRY."`.`countryAbbr`='".$_POST['country']."' WHERE `".NETWORKMASTER."`.networkId=`".NETWORKDETAILS."`.networkId  AND `".NETWORKMASTER."`.networkId=`".NETCOUNTRY."`.networkId AND `".NETWORKMASTER."`.networkId='".$_REQUEST['networkId']."'";
				
			$updatesql="UPDATE `".NETWORKMASTER."` SET `networkName` = '".$_POST['name']."',`networkType` = '".$_POST['type']."',`networkBidType`='".$_REQUEST['bidtype']."',`status`='".$_POST['status']."' WHERE `".NETWORKMASTER."`.networkId='".$_REQUEST['networkId']."'";
//echo $updatesql;
			$UPDATE=mysql_query($updatesql) or die('update NETMASTER '.mysql_error());
			
			$updatesql1="UPDATE `".NETWORKDETAILS."` SET `".NETWORKDETAILS."`.`networkMinPrice`='".$_POST['networkprice']."',`".NETWORKDETAILS."`.`costPerBidType`='".$_POST['bidcost']."' WHERE  `".NETWORKDETAILS."`.networkId='".$_REQUEST['networkId']."'";
//echo $updatesql1;
			$UPDATE=mysql_query($updatesql1) or die('update NETDETAILS'.mysql_error());
	
			$updatesql2="UPDATE `".NETCOUNTRY."`SET `".NETCOUNTRY."`.`countryAbbr`='".$_POST['country']."' WHERE  `".NETCOUNTRY."`.networkId='".$_REQUEST['networkId']."'";
//echo $updatesql2;
			$UPDATE=mysql_query($updatesql2) or die('updateNETCOUNTRY '.mysql_error());			


			//echo '<script> window.location="network.php?updt=1"</script>';
			}
	}
////////////////////////////////end////////////////////////////////////////////////////////
?>

<?php
$search="SELECT * FROM ".NETWORKMASTER ;

	if($_REQUEST['typesearch']!='' && isset($_REQUEST['typesearch']))
	{

		
		$search=$search." WHERE networkType like'".$_REQUEST['typesearch']."%'";
		
	
    	}
	if($_REQUEST['namesearch']!='' && isset($_REQUEST['namesearch']))
	{

		
		$search=$search." WHERE networkName like'".$_REQUEST['namesearch']."%'";
		
	
    	}
	if($_REQUEST['selectstatus']!='' && isset($_REQUEST['selectstatus']))
	{

		
		$search=$search." WHERE ".NETWORKMASTER.".status like'".$_REQUEST['selectstatus']."%'";
		
	
    	}
	elseif($_REQUEST['index']!='' && isset($_REQUEST['index']))
	{

		$param="index=".$_REQUEST['index'];
		$search=$search." WHERE networkName like'".$_REQUEST['index']."%'";
		
	
    	}
	
	else
	{	
	$search=$search." ORDER BY networkId  DESC";
	}
	$search=$search." limit $page,$limit";
	//echo $search;
	$searchquery=mysql_query($search) or die('error1'.mysql_error());

		
?>

 <table width="100%" border="0" cellspacing="0" cellpadding="3"  style="border:1px solid #666666">

 <tr><td>&nbsp;</td></tr>

 <tr><td>&nbsp;</td></tr>

 <tr><td>
 <table width="100%" border="0" cellspacing="0" cellpadding="3">
 <tr><td>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
		<tr> 
			<td height="15" colspan="2" align="center" style="color:#000000;font-size:12px;">
				<font color="#000000"><strong> NETWORK  MANAGEMENT </strong></font>
			</td> 
		</tr>

	<tr style="display:" id="editWin">
		<td colspan='2' id="ShowMsg">
		</td>
	</tr>
	<?php 
	if((isset($_GET['blk']) && $_GET['blk']==1) || (isset($_GET['dlt']) && $_GET['dlt']==1) || (isset($_GET['inst']) && $_GET['inst']==1) || (isset($_GET['updt']) && $_GET['updt']==1) )
	{
	?>
	<tr>
	
		<td colspan='2' style="color:#FF0000">
		<?php 
		if(isset($_GET['blk']) && $_GET['blk']==1)
		{
		echo $msg1="Value Blocked/Unblocked Successfully!!!";
		}
		if(isset($_GET['dlt']) && $_GET['dlt']==1)
		{
		echo $msg1="Value Deleted Successfully!!!";
		}
		if(isset($_GET['inst']) && $_GET['inst']==1)
		{
		echo $msg1="Value insert Successfully!!!";
		}
		if(isset($_GET['updt']) && $_GET['updt']==1)
		{
		echo $msg1="Value UPDATE Successfully!!!";
		}
		?></td>
	</tr>
	<?php
	}
	?>
</table>



	<table width="100%" border="0" cellspacing="0" cellpadding="3" class="tabbod">
	<?php
	if(isset($_REQUEST['action']))
		{
		$editsql="SELECT ".NETWORKMASTER.".networkId,".NETWORKMASTER.".networkName,".NETWORKMASTER.".networkType,".NETWORKMASTER.".networkBidType,".NETWORKMASTER.".status, ".NETWORKDETAILS.".networkMinPrice,".NETWORKDETAILS.".costPerBidType,".NETCOUNTRY.".countryAbbr FROM ".NETWORKMASTER.",".NETWORKDETAILS.",".NETCOUNTRY.",".NETADDETAILS.",".SITEMASTER." WHERE ".NETWORKMASTER.".networkId=".NETWORKDETAILS.".networkId  AND ".NETWORKMASTER.".networkId=".NETCOUNTRY.".networkId AND ".NETADDETAILS.".adsiteId=".SITEMASTER.".adsiteId AND ".NETWORKMASTER.".networkId='".$_REQUEST['networkId']."'";
//echo $editsql;
		$editquery=mysql_query($editsql);
		$fetch=mysql_fetch_array($editquery);
	?>
<tr><td colspan="7" id="edit_section" style="display:;">
<fieldset>
	<legend class="LEGEND"> <?php echo ucfirst($_REQUEST['action']);?>Network </legend>
	<form name="edit" action="" method="POST" onSubmit="return check();" >
	<input type="hidden" name="totalNoBox" id="totalNoBox">
		<table width="100%" border="0" align="center" cellpadding="5" cellspacing="0">
			<tr>
				<td width="17%" align="left" id="admin_text">Network Name</td>
				<td width="2%" align="left">::</td>
				<td width="81%" align="left"><input type="text" name="name" value="<?php echo $fetch['networkName'];?>" ></td>
			</tr>
			<tr>
				<td width="17%" align="left" id="admin_text">Network Type</td>
				<td width="2%" align="left">::</td>
				<td width="81%" align="left"><input type="text" name="type" value="<?php echo $fetch['networkType'];?>" ></td>
			</tr>
			<tr>
				<td width="17%" align="left" id="admin_text">Network Bid Type</td>
				<td width="2%" align="left">::</td>
				<td width="81%" align="left"><select name="bidtype" class="SMALL" >
										<option value="" selected="selected">Bid Type </option>
										<option value="CPC"  <?php if($fetch['networkBidType'] =="CPC") echo 'selected'; ?>>CPC</option>
										<option value="CPM"  <?php if($fetch['networkBidType'] =="CPM") echo 'selected'; ?>>CPM</option>
										
										</select>
				</td>
			</tr>
			<tr>
				<td width="17%" align="left" id="admin_text">Network Minimum Price</td>
				<td width="2%" align="left">::</td>
				<td width="81%" align="left"><input type="text" name="networkprice" value="<?php echo $fetch['networkMinPrice'];?>" ></td>
			</tr>
			<tr>
				<td width="17%" align="left" id="admin_text">Network Cost Per Bid Type</td>
				<td width="2%" align="left">::</td>
				<td width="81%" align="left"><input type="text" name="bidcost" value="<?php echo $fetch['costPerBidType'];?>" ></td>
			</tr>
			<tr>
				<td width="17%" align="left" id="admin_text" valign="top">
                            Add No Of Clicks to Purchase
                          </td>
				<td width="2%" align="left" valign="top">::</td>
				<td width="81%" align="left" valign="top"><div>
							&nbsp;<input type="button" value="Add" onclick="addRowToTable();" />
<input type="button" value="Remove" onclick="removeRowFromTable();" /></div>	
					<div><table border="0" id="tblSample">
					
					<tr>
						<td><input type="text" name="txtRow1"
						id="txtRow1" size="10" /></td>
						<td>
					</tr>
					</table></div>	


</td>
			</tr>
			<tr>
				<td width="17%" align="left" id="admin_text" valign="top">Network Country Abbreviation</td>
				<td width="2%" align="left">::</td>
				<td width="81%" align="left"><input type="text" name="country" value="<?php echo $fetch['countryAbbr'];?>" ></td>
			</tr>
<!--			<tr>
				<td width="17%" align="left" id="admin_text">Network Run On Site</td>
				<td width="2%" align="left">::</td>
				<td width="81%" align="left"><input type="text" name="networkprice" value="<?php echo $fetch['adSiteUrl'];?>" ></td>
			</tr>-->
			<tr>
				<td width="27%" align="left" id="admin_text">Status</td>
				<td width="10%" align="left">::</td>
				<td width="63%" align="left">Active
				<input type="radio" name="status" <?php if($fetch['status']=='Y'){?> checked="checked" <?php } ?> value="Y" >
				&nbsp;&nbsp;Inactive
				<input type="radio" name="status" <?php if($fetch['status']=='N'){?> checked="checked" <?php } ?> value="N" >
				&nbsp;&nbsp;Pending
				<input type="radio" name="status" <?php if($fetch['status']=='P'){?> checked="checked" <?php } ?> value="P" > 
				&nbsp;&nbsp;Block
				<input type="radio" name="status" <?php if($fetch['status']=='B'){?> checked="checked" <?php } ?> value="B" >       </td>
			</tr> 
			<tr>
				<td align="center">&nbsp;</td>
				<td align="center">&nbsp;</td>
				<td align="left"><?php if($_REQUEST['action']!=''){ ?><input type="submit" name="submit1" value="<?php if($_REQUEST['action']=='create'){echo INSERT;}else echo UPDATE;?>" /><?php } ?>
				&nbsp;
				<input type="button" value="Close" name="close_frm" id="close_frm" onClick="window.location='network.php'" /></td>
			</tr>
		</table>
	</form>
</fieldset>
	<?php } ?>
</td></tr>

<tr>

  <td height="15" colspan="7" align="center" style="color:#000000;font-size:12px;"><p><strong style="font-weight: 400">Click on the alphabets to browse Network name &nbsp;</strong></p><?php include_once("search_alphabets.php");?></td>

</tr>
<tr>

  <td height="15" colspan="7" align="left" style="color:#000000;font-size:12px;"><font face="Times New Roman, Times, serif" size="2"><strong>
			
		<a href="network.php?action=create"> Create Network </a></strong></font></td>
</tr>
<form name="search" method="POST" >
    <tr>
	<td colspan="6">
			<table width="100%" cellpadding="0" cellspacing="0" border="0">
				<tr>
					<td>Search By </td>
					<td valign="middle">Network Name :<input type="text" name="namesearch" value="">&nbsp;</td>					
					<td valign="middle">Network Type :<input type="text" name="typesearch" value="<?php echo $_POST['typesearch']; ?>"></td>
					<td class="SMALL"> Network Status:<select name="selectstatus" class="SMALL" >
						<option value="" selected="selected">Ad Status </option>
						<option value="Y"  <?php if($statuetype == "Y") echo 'selected'; ?>>Active</option>
						<option value="N"  <?php if($statustype == "N") echo 'selected'; ?>>Inactive</option>
						<option value="P"  <?php if($statustype == "P") echo 'selected'; ?>>Pending</option>
						<option value="B" <?php if($statustype == "B") echo 'selected'; ?>>Block</option>
					    </select>&nbsp;&nbsp;<input type="submit" name="search" value="Search">
					</td>
					<td class="SMALL"><a href="network.php" style="color:#000000;font-size:12px;">View All</a> </td>
				</tr>
				
			</table>
	</td>
    </tr>
</form>

<?php

  if(mysql_num_rows($searchquery)>0)
  	{
	
	?><form name="form_admin_management" action="" method="post">
		<tr bgcolor="#6F8DBC">
			<td width="7%" height="28" style="color:#ffffff;font-size:12px;"><strong>S.No</strong></td>
			<td width="32%" align="left" valign="middle" style="color:#ffffff;font-size:12px;"><strong>Network Name</strong> </td>
			<td width="32%" valign="middle" style="color:#ffffff;font-size:12px;"><strong>Network Type </strong></td>
			<td width="9%" align="center" valign="middle" style="color:#ffffff;font-size:12px;"><strong>Edit</strong></td>
			<td width="9%" align="center" valign="middle" style="color:#ffffff;font-size:12px;"><strong>Details</strong></td>
			<td width="11%" align="center" valign="middle" style="color:#ffffff;font-size:12px;"><strong>Status</strong></td>
			<td width="9%" align="center" valign="middle" style="color:#ffffff;font-size:12px;"><strong>Delete</strong></td>
	</tr>
	<?php 
			$count=0;
			$sno=1;
			$loop=0;
	while($searcharray=mysql_fetch_array($searchquery))
		{
			$loop++;
			if($loop == 1)
			$bgcolor = "#F0F0FF";
			else 
			$bgcolor = "#CCCCFF";
	?>
		<tr bgcolor="<?php echo $bgcolor; ?>">
			<td height="32" style="color:#000000;font-size:12px;"><strong><?php echo $sno+$_GET['page'];//echo $searcharray['usercode']; ?></strong></td>
			<td align="left" valign="middle" style="color:#000000;font-size:12px;"><?php echo ($searcharray['networkName']); ?></td>
			<td align="left" valign="middle" style="color:#000000;font-size:12px;"><?php echo ($searcharray['networkType']); ?></td>
			<td align="center" valign="middle"><a href="network.php?action=edit&networkId=<?php echo $searcharray['networkId'];?>" ><img src="images/edit_icon.gif" border="0"/></a></td>
			<td align="center" valign="middle"><a href="network.php?action=details&networkId=<?php echo $searcharray['networkId'];?>" ><img src="images/edit_icon.gif" border="0"/></a></td>
			<td align="center" valign="middle"><strong> <?php if($searcharray['status']=='Y')
										{
											echo "Active";
										}
									if($searcharray['status']=='N')
										{
											echo "In Active";
										}
									if($searcharray['status']=='P')
										{
											echo "Pending";
										}
									if($searcharray['status']=='B')
										{
											echo "Blocked";
										}
										
										?>
				</strong></td>
			<td align="center" valign="middle"><input type="checkbox" name="del[]" value="<?php echo $searcharray['networkId'];?>"></td>
		</tr>
			<?php 
			$count++;
			$sno++;
			if($loop == 2)
				{
				$loop = 0;
				}	
		}
			?>
		<tr>
			<td colspan="3">&nbsp;</td>
			<td colspan="4" align="right"><input type="hidden" name="count_UPDATE" value="<?php echo $count; ?>">
			<input type="submit" name="Submit" value="Update"></td>
		</tr>
		<tr>
			<td colspan="7" align="center" style="color:#FF0000"><?php echo paging($search,$page,$limit,'',$param);?></td>
		</tr>
		<?php
	
	}
	
	else
	
	{
	
	?>
	<tr bgcolor="#6F8DBC">
		<td width="7%" style="color:#ffffff;font-size:12px;"><strong>S.No</strong></td>
		<td width="32%" align="center" style="color:#ffffff;font-size:12px;"><strong>Network Name</strong></td>
		<td style="color:#ffffff;font-size:12px;"><strong>Network Type </strong></td>
		<td width="9%" align="center" style="color:#ffffff;font-size:12px;"><strong>Edit</strong></td>
		<td width="9%" align="center" style="color:#ffffff;font-size:12px;"><strong>Details</strong></td>
		<td width="11%" align="center" style="color:#ffffff;font-size:12px;"><strong>Block</strong></td>
		<td align="center" style="color:#ffffff;font-size:12px;"><strong>Delete</strong></td>
      </tr>
    <tr>
      <td colspan="7" align="left">&nbsp;&nbsp;&nbsp;<strong><font color="#FF0000" class="DISPLAYMESSAGES">No Result Found!</font></strong></td>
    </tr>
    <?php

  	}

  ?>
</form>
  </table>
  </td>
</tr>
</table>
<!--</fieldset>-->
</td>
</tr>
</table><script>
// Last updated 2006-02-21
function addRowToTable()
{
  var tbl = document.getElementById('tblSample');
  var lastRow = tbl.rows.length;
  // if there's no header row in the table, then iteration = lastRow + 1
  var iteration = lastRow+1;
  var row = tbl.insertRow(lastRow);
  
  // left cell
//   var cellLeft = row.insertCell(0);
//   var textNode = document.createTextNode(iteration);
//   cellLeft.appendChild(textNode);
  
  // right cell
  var cellRight = row.insertCell(0);
  var el = document.createElement('input');
  el.type = 'text';
  el.name = 'txtRow' + iteration;
  el.id = 'txtRow' + iteration;
  el.size = 10;
  
  el.onkeypress = keyPressTest;
  cellRight.appendChild(el);
	document.getElementById('totalNoBox').value=iteration;
  
  // select cell
//   var cellRightSel = row.insertCell(2);
//   var sel = document.createElement('select');
//   sel.name = 'selRow' + iteration;
//   sel.options[0] = new Option('text zero', 'value0');
//   sel.options[1] = new Option('text one', 'value1');
//   cellRightSel.appendChild(sel);
}
function keyPressTest(e, obj)
{
  var validateChkb = document.getElementById('chkValidateOnKeyPress');
  if (validateChkb.checked) {
    var displayObj = document.getElementById('spanOutput');
    var key;
    if(window.event) {
      key = window.event.keyCode; 
    }
    else if(e.which) {
      key = e.which;
    }
    var objId;
    if (obj != null) {
      objId = obj.id;
    } else {
      objId = this.id;
    }
    displayObj.innerHTML = objId + ' : ' + String.fromCharCode(key);
  }
}
function removeRowFromTable()
{
  var tbl = document.getElementById('tblSamplee');
  var lastRow = tbl.rows.length;
  if (lastRow > 1) tbl.deleteRow(lastRow - 1);
}

</script>
<?php 

include("foot.php");?>